package com.zd.controller;


import com.github.pagehelper.PageInfo;
import com.zd.model.*;
import com.zd.service.*;
import com.zd.utils.DateUtil;
import com.zd.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/practiceManageInfo")
public class PracticeManageInfoController extends BaseController {


    @Autowired
    private IPracticeManageInfoService practiceManageInfoService;//all

    @Autowired
    private IDepartmentService departmentService;//学院

    @Autowired
    private IMajorService majorService;//专业

    @Autowired
    private IPracticeTypeService practiceTypeService;//实习类别

    @Autowired
    private IPracticePropertyService practicePropertyService;//实习性质

    /**
     * 企业实践查询
     *
     * @return
     */
    @RequestMapping(value = "/queryPracticeManageInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String queryPracticeManageInfo(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                                  int pageNum,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                                  int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {
        /*//添加录入人查询条件,排除部门为11的用户人员，其他只能看到自己提交的数据
        HttpSession session = getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo.getDepartmentid() != 11) {
            queryMap.put("enteruserid", userInfo.getId());
        }*/
        List<PracticeManageInfo> practiceManageInfoList = practiceManageInfoService.selectPracticeManageInfo(pageNum, pageSize, queryMap);
        model.addAttribute("pageInfo", new PageInfo<>(practiceManageInfoList));
        model.addAttribute("practiceManageInfoList", practiceManageInfoList);
        model.addAttribute("queryMap", queryMap);
        return "practiceManageInfoIndex";
    }

    /**
     * 企业实践统计
     *
     * @param statisticsMap
     * @return
     */
    @ResponseBody
    @PostMapping("/selectPracticeStatistics")
    public List<PracticeStatistics> selectPracticeStatistics(@RequestParam Map<String, Object> statisticsMap){
        return practiceManageInfoService.selectPracticeStatistics(statisticsMap);
    }

    @GetMapping("/practiceManageInfoStatistics")
    public String PracticeManageInfoStatistics() {

        return "practiceManageInfoStatistics";
    }
    /**
     * 跳转企业实践信息添加页面
     *
     * @return
     */
    @GetMapping("/toPracticeManageInfoAdd")
    public String toPracticeManageInfoAdd() {
        return "practiceManageInfoAdd";
    }

    /**
     * 企业添加实践信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/practiceManageInfoAdd")
    public boolean practiceManageInfoAdd(PracticeManageInfo practiceManageInfo) {
        //入库时间
        practiceManageInfo.setEntertime(new Date());
        //录入用户id
        HttpSession session = getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        practiceManageInfo.setEnteruserid(userInfo.getId());
        //插入工作信息表
        practiceManageInfoService.insertSelective(practiceManageInfo);
        return true;
    }


    /**
     * 查看详细
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toPracticeManageInfoDetail")
    public String toPracticeManageInfoDetail(int id, Model model) {

        PracticeManageInfo practiceManageInfo = practiceManageInfoService.selectPracticeManageInfoById(id);

        model.addAttribute("practiceManageInfo", practiceManageInfo);

        return "practiceManageInfoDetail";

    }

    /**
     * 跳转修改页
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toPracticeManageInfoUpdate")
    public String toPracticeManageInfoUpdate(int id, Model model) {

        PracticeManageInfo practiceManageInfo = practiceManageInfoService.selectPracticeManageInfoById(id);

        model.addAttribute("practiceManageInfo", practiceManageInfo);

        return "practiceManageInfoUpdate";
    }


    /**
     * 企业修改实践
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/practiceManageInfoUpdate")
    public boolean practiceManageInfoUpdate(PracticeManageInfo practiceManageInfo) {
        practiceManageInfoService.updateSelective(practiceManageInfo);
        return true;
    }

    /**
     * 逻辑删除校企实践表
     *
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("deletePracticeManageInfo")
    public boolean deletePracticeManageInfo(int id) {

        practiceManageInfoService.deletePracticeManageInfo(id);

        return true;
    }

    /**
     * 导出excel
     *
     * @param queryMap
     * @return
     */

    @PostMapping("exportExcel")
    public void exportExcel(@RequestParam Map<String, Object> queryMap, HttpServletResponse response) {

        //标题
        String titleName = "校企合作工作台账(以";
        //excel拼查询条件
        //学院
        if (queryMap.get("departmentid") != null && Integer.valueOf((String) queryMap.get("departmentid")).intValue() != -1) {
            Department department = departmentService.selectByPrimaryKey(Integer.valueOf((String) queryMap.get("departmentid")).intValue());
            titleName += "学院是" + department.getDepartment() + ",";
        }
        //计划时间
        if (queryMap.get("practicetimeplan") != null && !queryMap.get("practicetimeplan").equals("")) {
            titleName += "计划时间是" + queryMap.get("practicetimeplan") + ",";
        }
        //实际时间
        if (queryMap.get("practicetimeactual") != null && !queryMap.get("practicetimeactual").equals("")) {
            titleName += "实际时间是" + queryMap.get("practicetimeactual") + ",";
        }
        //基地名称
        if (queryMap.get("basename") != null && !queryMap.get("basename").equals("")) {
            titleName += "基地名称是" + queryMap.get("basename") + ",";
        }
        //计划人数
        if (queryMap.get("peoplenumplan") != null && !queryMap.get("peoplenumplan").equals("")) {
            titleName += "计划人数是" + queryMap.get("peoplenumplan") + ",";
        }
        //实际人数
        if (queryMap.get("peoplenumactual") != null && !queryMap.get("peoplenumactual").equals("")) {
            titleName += "实际人数是" + queryMap.get("peoplenumactual") + ",";
        }
        //计划内容
        if (queryMap.get("contentplan") != null && !queryMap.get("contentplan").equals("")) {
            titleName += "计划内容是" + queryMap.get("contentplan") + ",";
        }
        //实际内容
        if (queryMap.get("contentactual") != null && !queryMap.get("contentactual").equals("")) {
            titleName += "实际内容是" + queryMap.get("contentactual") + ",";
        }
        //计划校内指导教师
        if (queryMap.get("teachernameplan") != null && !queryMap.get("teachernameplan").equals("")) {
            titleName += "计划校内指导教师是" + queryMap.get("teachernameplan") + ",";
        }
        //实际校内指导教师
        if (queryMap.get("teachernameactual") != null && !queryMap.get("teachernameactual").equals("")) {
            titleName += "实际校内指导教师是" + queryMap.get("teachernameactual") + ",";
        }
        //实际创业指导教师
        if (queryMap.get("teachernameactualentrepreneurship") != null && !queryMap.get("teachernameactualentrepreneurship").equals("")) {
            titleName += "实际创业指导教师是" + queryMap.get("teachernameactualentrepreneurship") + ",";
        }
        //专业
        if (queryMap.get("majorid") != null && Integer.valueOf((String) queryMap.get("majorid")).intValue() != -1) {
            Major major = majorService.selectByPrimaryKey(Integer.valueOf((String) queryMap.get("majorid")).intValue());
            titleName += "专业是" + major.getMajorname() + ",";
        }
        //年级班级
        if (queryMap.get("classes") != null && !queryMap.get("classes").equals("")) {
            titleName += "年级和班级是" + queryMap.get("classes") + ",";
        }
        //实习类别
        if (queryMap.get("practicetypeid") != null && Integer.valueOf((String) queryMap.get("practicetypeid")).intValue() != -1) {
            PracticeType practiceType = practiceTypeService.selectByPrimaryKey(Integer.valueOf((String) queryMap.get("practicetypeid")).intValue());
            titleName += "实习类别是" + practiceType.getType() + ",";
        }
        //实习性质
        if (queryMap.get("practicepropertyid") != null && Integer.valueOf((String) queryMap.get("practicepropertyid")).intValue() != -1) {
            PracticeProperty practiceProperty = practicePropertyService.selectByPrimaryKey(Integer.valueOf((String) queryMap.get("practicepropertyid")).intValue());
            titleName += "实习性质是" + practiceProperty.getProperty() + ",";
        }

        titleName += "来统计)";

        //列名
        String[] columnName = {"学院", "计划时间", "实际时间", "基地名称", "计划人数", "实际人数", "计划内容", "实际内容", "计划校内指导教师", "实际校内指导教师", "实际创业指导教师",
                "专业", "年级和班级", "实习类别", "实习性质"};

        //获取企业实践数据
        List<PracticeManageInfo> practiceManageInfoList = practiceManageInfoService.selectPracticeManageExport(queryMap);

        if (practiceManageInfoList != null && practiceManageInfoList.size() != 0) {

            String[][] dataList = new String[practiceManageInfoList.size()][columnName.length];

            for (int i = 0; i < practiceManageInfoList.size(); i++) {

                PracticeManageInfo practiceManageInfo = practiceManageInfoList.get(i);

                //学院
                dataList[i][0] = practiceManageInfo.getDepartment().getDepartment();
                //计划时间
                dataList[i][1] = DateUtil.dateToStr(practiceManageInfo.getPracticetimeplan());
                //实际时间
                dataList[i][2] = DateUtil.dateToStr(practiceManageInfo.getPracticetimeactual());
                //基地名称
                dataList[i][3] = practiceManageInfo.getBasename();
                //计划人数
                dataList[i][4] = practiceManageInfo.getPeoplenumplan().toString();
                //实际人数
                dataList[i][5] = practiceManageInfo.getPeoplenumactual().toString();
                //计划内容
                dataList[i][6] = practiceManageInfo.getContentplan();
                //实际内容
                dataList[i][7] = practiceManageInfo.getContentactual();
                //计划校内指导教师
                dataList[i][8] = practiceManageInfo.getTeachernameplan();
                //实际校内指导教师
                dataList[i][9] = practiceManageInfo.getTeachernameactual();
                //实际创业指导教师
                dataList[i][10] = practiceManageInfo.getTeachernameactualentrepreneurship();
                //专业
                dataList[i][11] = practiceManageInfo.getMajor().getMajorname();
                //年级班级
                dataList[i][12] = practiceManageInfo.getClasses();
                //实习类别
                dataList[i][13] = practiceManageInfo.getPracticeType().getType();
                //实习性质
                dataList[i][14] = practiceManageInfo.getPracticeProperty().getProperty();

            }

            //生成文件名
            String fileName = UUID.randomUUID() + ".xls";

            try {
                //导出excel
                ExcelUtil.exportWithResponse(titleName, columnName, dataList, fileName, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

